using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Socium.Presentation.TreeViews {
	public static class TreeViewHelper {
		public static TreeViewItem GetNearestContainer(object obj) {
			return GetNearestContainer(obj as UIElement);
		}

		public static TreeViewItem GetNearestContainer(UIElement element) {
			// Walk up the element tree to the nearest tree view item.
			var container = element as TreeViewItem;
			while ((container == null) && (element != null)) {
				element = VisualTreeHelper.GetParent(element) as UIElement;
				container = element as TreeViewItem;
			}

			return container;
		}

		public static TreeViewItem GetSelectedItem(this TreeView tree_view) {
			return (TreeViewItem) tree_view.ItemContainerGenerator.ContainerFromIndex(tree_view.Items.CurrentPosition);
		}
	}
}